public class Main {
    public static void main(String[] args) {
        System.out.println("Hello world!");
    }
}
class Solution {
    public List<List<Integer>> levelOrder(TreeNode root) {
        //结果数组
        List<List<Integer>> ret = new ArrayList<List<Integer>>();
        Queue<TreeNode> queue = new LinkedList<>();
        if(root == null){
            return ret;//必须返回空的二维数组
        }
        queue.offer(root);
        while(!queue.isEmpty()){
            //每层数据
            List<Integer> level = new ArrayList<Integer>();
            int size = queue.size();
            //根据每层元素数循环
            while(size > 0){
                TreeNode cur = queue.poll();
                level.add(cur.val);
                if(cur.left != null){
                    queue.offer(cur.left);
                }
                if(cur.right != null){
                    queue.offer(cur.right);
                }
                size--;
            }
            ret.add(level);
        }
        return ret;
    }
}
class Solution2{
    public List<List<Integer>> levelOrderBottom(TreeNode root) {
        List<List<Integer>> ret = new ArrayList<>();
        Queue<TreeNode> queue = new LinkedList<>();
        if(root == null){
            return ret;//必须返回空的二维数组
        }
        queue.offer(root);
        while(!queue.isEmpty()){
            //每层数据
            List<Integer> level = new ArrayList<Integer>();
            int size = queue.size();
            //根据每层元素数循环
            while(size > 0){
                TreeNode cur = queue.poll();
                level.add(cur.val);
                if(cur.left != null){
                    queue.offer(cur.left);
                }
                if(cur.right != null){
                    queue.offer(cur.right);
                }
                size--;
            }
            ret.addFirst(level);
        }
        return ret;
    }
}